//
//  SEGActivityLotteryBreakEggsCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/16.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryBreakEggsCell.h"
#import "SEGActivityLotteryBreakEggsView.h"

@interface SEGActivityLotteryBreakEggsCell ()

@property (nonatomic, strong) UIButton* restCountButton; //剩余次数
@property (nonatomic, strong) UIImageView* bgImageView; //抽奖背景

/**
 砸金蛋视图
 */
@property (nonatomic, strong)SEGActivityLotteryBreakEggsView *breakEggsView;
/**
 点击的金蛋序号
 */
@property (nonatomic, assign) NSInteger eggIndex;

@end

@implementation SEGActivityLotteryBreakEggsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = [UIColor colorWithRed:210/255.0 green:59/255.0 blue:48/255.0 alpha:1];
    
    self.bgImageView.hidden = NO;
    self.restCountButton.hidden = NO;
    
    SEGAdjustableButton *winRecordButton = [[SEGAdjustableButton alloc] init];
    UIImage *img = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity01_gift")];
    [winRecordButton setImage:img forState:UIControlStateNormal];
    //    [winRecordButton setTitle:@"我的中奖记录 >" forState:UIControlStateNormal];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"我的中奖记录 >"];
    NSRange strRange2 = {0,[str2 length]};
    [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange2];
    [str2 addAttribute:NSForegroundColorAttributeName value:UIColor.whiteColor range:strRange2];
    [winRecordButton setAttributedTitle:str2 forState:UIControlStateNormal];
    
    [winRecordButton setTitleColor:kColor_W forState:UIControlStateNormal];
    winRecordButton.titleLabel.font = kFont_13;
    [winRecordButton addTarget:self action:@selector(seg_winRecordButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:winRecordButton];
    [winRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.bgImageView.mas_bottom).offset(29).priorityHigh();
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-29);
    }];
    [winRecordButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:6];

    [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        // iOS10 以上的适配
//        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
//            make.left.top.right.equalTo(self);
//            make.bottom.equalTo(winRecordButton.mas_bottom).mas_offset(29);
//        }else{
            make.edges.equalTo(self);
//        }
    }];
    
}

- (void)setUpCellWithLotteryDetailModel:(SEGActivityLotteryDetailModel *)model actStatus:(SEGActivityStatus)actStatus
{
    if (actStatus == SEGActivityStatus_Over) {
        [self.restCountButton setTitle:@"  亲，活动已经结束啦！ " forState:UIControlStateNormal];
    }else if([model.freeLotterys intValue] > 0) {
        [self.restCountButton setTitle:[NSString stringWithFormat:@"  您今天有 %d 次免费抽奖机会  ",[model.freeLotterys intValue]] forState:UIControlStateNormal];
    }else if([model.lotteryTimes intValue] > 0){
        [self.restCountButton setTitle:[NSString stringWithFormat:@"  您今天还有 %d 次额外抽奖的机会，%d 积分每次。 ",[model.lotteryTimes intValue], [model.price intValue]] forState:UIControlStateNormal];
    }else {
        [self.restCountButton setTitle:@"  亲，您的抽奖机会已经用完！ " forState:UIControlStateNormal];
    }
    
    // 开始砸金蛋锤子移动动画（处理抽奖之后动画停止的情况）
    [self.breakEggsView startHummerMoveAnimation];
}

- (void)startHummerMoveAnimation {
    // 开始砸金蛋锤子移动动画（处理抽奖之后动画停止的情况）
    [self.breakEggsView startHummerMoveAnimation];
}

- (void)seg_winRecordButtonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(goToWinRecordViewController)]) {
        [self.delegate goToWinRecordViewController];
    }
}

- (void)seg_beginLottery
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(lotteryBegin)]) {
        [self.delegate lotteryBegin];
    }
}

#pragma mark - getter
- (UIButton *)restCountButton
{
    if (!_restCountButton) {//img_activity01_newbg
        _restCountButton = [[UIButton alloc] init];
        [_restCountButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _restCountButton.titleLabel.font = kFont_13;
        
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_cishubg")];
        CGFloat height = image.size.height;
        CGFloat width = image.size.width;
        UIEdgeInsets insets = UIEdgeInsetsMake(height / 2, width / 2, height / 2, width / 2);
        UIImage *image2 = [[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_cishubg")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        [_restCountButton setBackgroundImage:image2 forState:UIControlStateNormal];
        [self.contentView addSubview:_restCountButton];
        [self.contentView bringSubviewToFront:_restCountButton];
        [_restCountButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.contentView).offset(15);
//            make.width.mas_equalTo(159);
            make.height.mas_equalTo(30);
        }];
    }
    return _restCountButton;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_bg")];
        [self.contentView addSubview:_bgImageView];
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.centerX.equalTo(self);
            make.width.mas_equalTo(LYScreenWidth());
            make.height.mas_equalTo(375 * LYScreenScaleWidth());
        }];
        _bgImageView.userInteractionEnabled = YES;
        
        if (!self.breakEggsView) {
            self.breakEggsView = [[SEGActivityLotteryBreakEggsView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 375 * LYScreenScaleWidth()) finished:^(NSInteger index) {
                self.eggIndex = index;
                [self seg_beginLottery];
            }];
            [_bgImageView addSubview:self.breakEggsView];
        }
        
    }
    return _bgImageView;
}

- (void)startAnimationWithResult:(BOOL)hasWin
{
    [self.breakEggsView startAnimatin:hasWin eggIndex:self.eggIndex];
}

@end
